#[macro_use]
extern crate clap;
use clap::{SubCommand, App, Arg, AppSettings};
use doorer::config::Config;
use doorer::activate::Activate;
use doorer::utils::{update};
use doorer::open::Open;
use doorer::tools::Tools;

fn main() {
    let matches = App::new(crate_name!())
        .about(crate_description!())
        .version(crate_version!())
        .author(crate_authors!())
        .subcommand(SubCommand::with_name("config")
            .alias("c")
            .about("配置编程语言国内源, 简称 c")
            .help("配置国内源")
            .arg(Arg::with_name("language")
                .takes_value(true)
                .required(false)))
        .subcommand(SubCommand::with_name("activate")
            .alias("a")
            .about("激活软件,简称 a")
            .help("激活")
            .arg(Arg::with_name("software")
                .takes_value(true)
                .required(false)))
        .subcommand(SubCommand::with_name("upgrade")
            .alias("u")
            .about("doorer升级, 简称 u")
            .help("doorer升级")
        )
        .subcommand(SubCommand::with_name("open")
            .alias("o")
            .about("打开文件, 简称 o")
            .help("打开文件")
            .arg(Arg::with_name("file")
                .takes_value(true)
                .required(false))
        )
        .subcommand(SubCommand::with_name("tools")
            .alias("t")
            .about("小工具, 简称 t")
            .help("小小工具箱")
        )
        .setting(AppSettings::SubcommandRequiredElseHelp) // 无参数 打印帮助文档
        .get_matches();

    // 配置
   if let Some(language) = matches.subcommand_matches("config") {
       let lang = language.value_of("language").unwrap_or_default();
       let lang = lang.to_lowercase();
       let lang = lang.as_str();

       let config = Config{};
       config.run(lang);

   }

    // 激活
    if let Some(software ) = matches.subcommand_matches("activate") {
        let soft = software.value_of("software").unwrap_or_default();
        let soft = soft.to_lowercase();
        let soft = soft.as_str();

        let activate = Activate{};
        activate.run(soft);
    }

    // 检测升级
    if matches.is_present("upgrade") {
        println!("当前版本号为：{}",crate_version!());
        // 获取github latest 最新版本号
        update(crate_version!());
    }


    // 打开文件
    if let Some(file) = matches.subcommand_matches("open") {
        let filename = file.value_of("file").unwrap_or_default();
        let filename = filename.to_lowercase();
        let filename = filename.as_str();
        // 执行
        let open = Open{};
        open.run(filename);
    }

    // 小小工具箱
    if matches.is_present("tools") {
        let tools = Tools{};
        tools.run();
    }

}